package com.bjc.platform.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSONResult {
	private Map<String,Object> jsonResult;
	
	public JSONResult(boolean success,Object results,int count){
		super();
		jsonResult=new HashMap<String,Object>();
		jsonResult.put("results", results);
		jsonResult.put("success", success);
		jsonResult.put("totalCount",count);
	}
	
	public JSONResult(boolean success,Object results,Long count){
		super();
		jsonResult=new HashMap<String,Object>();
		jsonResult.put("results", results);
		jsonResult.put("success", success);
		jsonResult.put("totalCount",count);
	}
	
	public JSONResult(boolean success,Object results){
		super();
		jsonResult=new HashMap<String,Object>();
		if(results instanceof OperationMessage){
			OperationMessage message=(OperationMessage)results;
			jsonResult.put("message", message.getDesc());
		}else{
			jsonResult.put("results", results);
		}
		jsonResult.put("success", success);
		jsonResult.put("totalCount",1);
	}
	
	/**
	 * 获得最终的结果对象
	 * @return
	 */
	public Object  getJSONResult(){
		return this.jsonResult;
	}
	
	public List<?> getDatas(){
		return (List<?>)this.jsonResult.get("results");
	}
	
	public Object getData(){
		return this.jsonResult.get("results");
	}
	
	public void setDatas(Object datas){
		this.jsonResult.put("results", datas);
	}
	
	public int getCount(){
		if(this.jsonResult.get("totalCount")!=null){
			int count=Integer.parseInt(this.jsonResult.get("totalCount").toString());
			return count;
		}else{
			return 0;
		}
	}
	
	public boolean addData(Object data){
		@SuppressWarnings("unchecked")
		List<Object> datas=(List<Object>) this.getDatas();
		return datas.add(data);
	}
}
